//一加载脚本就需要初始化的函数，避免对象不存在的错误。
(function(){
/**
 * 添加样式
 */
coos.style = {
	add : function(content)
	{
		var style = null;
	    if(document.all)
	    {
	        style = document.createStyleSheet();
	        style.cssText = content;
	    }
	    else
	    {
	        style = document.createElement("style");
	        style.type = "text/css";
	        //style.innerHTML = content;//Safari、Chrome 下innerHTML只读
	        style.textContent = content;
	        try
	        {
	    		document.getElementsByTagName("head")[0].appendChild(style);
	    	}
	    	catch(e){}
	    }
	}
};

/**
 * 提供url常用操作功能
 * random 在url后面加入随机数参数
 * getAttribe 获得url中的参数值
 * replaceAttribe 替换url中的参数值
 * addAttribe 添加参数
 * getAction 获得url中action的名字(后缀可变)
 * location url跳转
 */
coos.url = {
	random : function(url)
	{
		if (url.indexOf ("?") > 0)
		{
			url = url + "&random=" + new Date().getTime();
		}
		else
		{
			url = url + "?random=" + new Date().getTime();
		}
		return url;
	},
	getAttribe : function(type,url)
	{
		var url = url || location.href;
		var urltemp = url.split(type + "=")[1];
		if(!urltemp) return "";
		if(urltemp.indexOf("&") == -1) 
		{
			return urltemp;
		}
		else
		{
			return urltemp.split("&")[0];
		}
	},
	replaceAttribe : function(type,value,url)
	{
		var url = url || location.href;
		
		var oReg = new RegExp(type + "=([^&]+)?","g");
		
		url= url.replace(oReg, type + "=" + value);
		return url;
	},
	addAttribe : function(type,value,url)
	{
		var url = url || location.href;
		if(url.indexOf ("?") == -1)
		{
			return (url + "?" + type + "=" + value);
		}
		else
		{
			return (url + "&" + type + "=" + value);
		}
	},
	getAction : function(url,action)
	{
		var url = url || location.href;
		var action = action || ".action";
		var temp = url.split(action)[0];
		return temp.substring(temp.lastIndexOf("/")+1,temp.length);
	},
	location : function(url,message)
	{
		var url = url || location.href;
		if(!message)
		{
			window.location = url;
			return false; 
		}
		if(confirm(message))
		{
			window.location = url;
		}
		return false; 
	}
};

/**
 * iframe的处理函数
 * 提供批量和单独iframe自适应高度功能
 */
coos.iframe = {
	autoHeights : function(els)
	{
		for (var i = 0; i < arguments.length; i++) 
		{
	        coos.iframe.autoHeight(arguments[i]);
	    }
	},
	autoHeight : function(el)
	{
		var obj = coos.$obj(el);
		var id = obj.id;
		var subWeb = document.frames ? document.frames[id].document : obj.contentDocument;   
		if(obj != null && subWeb != null)
		{
			//iframe body 设置为height:100%;时火狐下最小高度为150
			obj.height = parseInt(subWeb.body.scrollHeight) + "px";
		   
		}
	},
	onload : function(el,fn)
	{
		var ifr = coos.$obj(el);
		//IE中iframe只支持隐形的onload事件,需要通过attachEvent来注册，ie的readystatechange事件有一些潜在的问题
		if(ifr.attachEvent)
		{
			ifr.attachEvent("onload",fn);
		}
		else
		{
			ifr.onload = fn;
		}
	}
};

/**
 * cookie的处理函数
 */
coos.cookie = {
	set : function(name,value,days,path,domain,secure)
	{
		var days = days || 100;
		var path = path || "/";
		var domain = domain || "";
		var secure = secure || "";
	    var exp  = new Date();
	    exp.setTime(exp.getTime() + days*24*60*60*1000);
	    document.cookie = name + "="+ escape (value) 
	    + ";expires=" + exp.toGMTString()
	    + ";path="+ path
	    + (domain == "" ? "" : ("; domain=" + domain))
	    + (secure ? "; secure" : "");
	},
	get : function(name)
	{
		var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
		if(arr != null)
			return unescape(arr[2]);
		return null;
	},
	del : function(name)
	{
		var exp = new Date();
	    exp.setTime(exp.getTime() - 1);
	    var cval=this.get(name);
	    if(cval!=null)
	    	document.cookie= name + "="+cval+";expires="+exp.toGMTString();
	}
};

/**
 * form相关的通用函数
 */
coos.form = {
	submit : function(formElement,submitId)
	{
		if(!coos.$obj(formElement))return false;
		var submitId = submitId || "subButton";
		coos.$id(submitId).disabled = true;
		coos.$obj(formElement).submit();
		return false;
	},
	update : function(formElement,formName,action)
	{
		if(formName)
			formElement.name = formName;
		if(action)
			formElement.action = action;
	},
	validate : function(){}
};

})();
//初始化函数结束

